<template>
  <div class="app-container">
    <div class="filter-container">
      <Warn />
    </div>
    <div class="filter-container">
      <el-button
        v-waves
        type="primary"
        icon="el-icon-circle-plus-outline"
        class="filter-item"
        size="small"
        @click="handleCreate"
        disabled
      >添加分类</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :border="false"
      fit
      style="width: 100%;"
    >
      <el-table-column label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="200">
        <template slot-scope="{row}">
          <el-input v-model.trim="row.name" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <span>
            <el-switch v-model="row.statusForm" />
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-button type="success" size="mini" icon="el-icon-edit" @click="handleSave(row)">
            保存
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(row)" disabled>
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import api from '@/api'

import Warn from './components/Category'
export default {
  name: 'AdCategory',
  directives: { waves },
  components: {
    Warn
  },
  data() {
    return {
      listLoading: false,
      list: []
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    /**
     * 获取数据列表
     */
    getList() {
      this.listLoading = true
      api.systemSwiper.listCategory().then(response => {
        const { data } = response
        this.list = data
        this.listLoading = false
        this.list.forEach(x => {
          // 状态处理
          const statusForm = +x.status === 1
          this.$set(x, 'statusForm', statusForm)
        })
      }).finally(() => {
        this.listLoading = false
      })
    },
    /**
     * 新建
     */
    handleCreate() {
      this.list.push({
        id: 0,
        name: '',
        status: 1,
        statusForm: true
      })
    },
    /**
     * 删除
     * @param row
     */
    handleDelete(row) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.systemSwiper.removeCategory({ id: row.id }).then(response => {
          this.$message({
            message: response.message || '删除成功',
            type: 'success'
          })
          this.list = this.list.filter(x => +x.id !== +row.id)
        })
      }).catch(() => {})
    },
    /**
     * 状态设置
     */
    handleSave(row) {
      api.systemSwiper.saveCategory({
        ...row,
        status: row.statusForm ? 1 : 0
      }).then(response => {
        this.$set(row, 'id', response.data.id)
        this.$notify({
          title: '成功',
          message: response.message || '设置成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
